Repository navigation
CBv2: 1,024-token historical checkpoints, demand-targeted retention, slot-wide staging cap - #168
Merged
Merged
Conversation
Historical (attention-only) layouts no longer require every computed range below a checkpoint to share one chunk cap. A checkpoint at p is the full-attention rows [0, p) plus each sliding owner's [p-W, p), written with absolute positions, so any stride-aligned range end is a complete model state whatever chunk size produced it. `recordHistorical` captures at every multiple of `historicalCheckpointStrideTokens` (1,024) a range covers; a request that gains or loses decode company mid-prompt keeps capturing. Packed rows, a non-contiguous range and preemption still disarm. Recurrent layouts keep `record` and its uniform-chunk rule unchanged. Per donor the capture stages up to K = 8 historical checkpoints (first plus the deepest K-1, shallowest interior retired first) under a per-request byte budget of 1/16 of the admission capacity, never below two. Measured window reservation per checkpoint: gpt-oss-20b 4.46 MB, gemma-4-26b 216 MB. Historical manifests carry the stride as `chunkSize`; import no longer range-checks it against the scheduler and the adopter is not forced onto the donor's chunk geometry (solo stripe and first-token projection apply). Historical donors publish deepest first. `CBv2InFlightStep.historicalCheckpoints` holds a list per request.
A 2,048-token solo stripe covers a 1,900-token prompt in one range, so a stride-aligned checkpoint needs to land strictly inside the range. The paged ring keeps max(window + speculative span, maxPrefillChunk) tokens behind the highest written position (2,048 for gpt-oss and gemma-4), so after a stripe [a, b) the sliding rows [p - W, p) for p = a + 1,024 are still resident (gpt-oss: p - 128 >= b - 2,048; gemma-4: exactly at the stripe's own alignment). `CBv2HistoricalWindow` now accepts a position at or below the row frontier and keeps refusing anything older than `oldestValidPosition`, so an evicted interior window is skipped rather than copied stale. Full-attention pages are immutable below the frontier; the window gathers stay in the launching step's asyncEval, before any successor may write the ring. Engine tests restore from interior boundaries (3c inside [2c, 4c), 5c inside a ragged tail) and prove residency accept/refuse on the tiny ring and at production gpt-oss/gemma-4 window geometry.
Replace the K = 8 retention with at most three checkpoints per historical donor: the first stride boundary, the fork target the coordinator's repeated-prefix hint names, and the rolling latest. A checkpoint is worth its copy and its file only where a later request will share exactly that prefix: the end of the prompt (the next turn) and a fork point (a shared preamble, agent sub-tasks, regenerations). K = 8 wrote 2.6x the bytes of the older first/latest rule and held the donor's terminal and KV behind every file. - CBv2Request.prefixCheckpointTargetTokens carries the hint (nil without one, 0 for a fleet-novel prompt); target = floor_stride(hint). - CBv2HistoricalCheckpointRetention holds the rule over positions. A target within one stride of the final deepest boundary is dropped at publication. An adopter restored at M captures only above M: no first, and a target only above M. - Only boundaries retention will keep are copied: the deepest capturable boundary of a range first, then an open first and the target below it. - The staged byte budget reads the admission capacity at every commit. Over budget the target is given up; the first/latest pair is never split. - Publication stays deepest first, then target, then first. Recurrent layouts are unchanged. Historical adopters stopped carrying a recurrent chunk size, which was what kept them out of rectangular packed prefill. Keep them out with an explicit CBv2PrefixReusePlan.excludesPackedPrefill; the engine test packs two cold rows as its control and fails without the flag.
Staged checkpoint windows and request reservations draw on one admission ledger. A retained checkpoint serves a future request; a reservation is a request being served now. Nothing bounded staged windows across donors, so four concurrent gemma-4 donors could hold 3.46 GB of a 16 GB slot. prepareHistorical now refuses a boundary (nil, the ordinary graceful path) when every donor's staged windows plus this one's would exceed 1/8 of the slot's admission capacity, read at that moment because a slot can be re-sliced at runtime. The window bytes come from the allocation-free reservation figure, so a refused boundary builds no graph and reserves nothing. Within one donor the claim order is rolling latest, then fork target, then first: attempts are made in that order and a donor gives up its own first, then its target, for a higher claim. A donor at the cap still rolls its latest, since the replacement does not raise the total. Donors do not displace each other. In-flight candidates are counted net of the latest they replace and reset when the next capturing step prepares; a step holding candidates never chains a successor. The recurrent capture path is unchanged and has no slot-wide cap. Tests: cap arithmetic and shedding order; N donors with the next refused, admitted after a release, and a donor rolling at the cap; target before first with room for one; the same reservation fitting beside capped windows and exhausting the ledger beside uncapped ones; an engine slot that admits a concurrent request only when the cap holds; a preempted donor releasing its staged windows, retention state and transient charge.
The per-donor budget (1/16 of capacity) gave up the fork target and kept the first/latest pair, while the slot-wide cap sheds the first before the target. On an 8 GB gemma-4 slot the per-donor budget binds first, so donors there lost the one boundary with demand evidence behind it and kept a guessed first. Both bounds now claim in the same order: rolling latest, then fork target, then first. Over the budget the first goes, then the target, and the latest is always kept. Without a target (no hint, hint 0, target not above the first, target at or beyond the latest) the pair first + latest is kept exactly as before. `dropInterior` is gone; the budget path uses the same `sheddable`/`shed` the slot-wide cap uses, excluding the retained latest.
`publish` moved a donor's captures out of `staged`, but their window reservations are released only when the publication batch closes after the last file is written. The cap summed `staged` alone, so while a gemma-4 donor wrote its files new donors could stage up to the full cap on top of them: four donors finishing together beside four new ones reached about 5.2 GB on a 16 GB slot, above the pre-change worst case. The capture now keeps a lock-protected per-request tally of window bytes in publication, added by `publish` on the engine queue and cleared by the batch's close on the retirement queue once the captures have released their reservations; `stagedHistoricalBytes` sums staged and publishing. Recurrent captures hold no window bytes and add nothing. The discard path (`step.discard`, preemption, a failed copy) now also takes the candidate out of the in-flight tally through `discardHistorical`, instead of relying on the reset at the next capturing step.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
This was referenced Sep 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
CBv2: 1,024-token historical checkpoints, demand-targeted retention, slot-wide staging cap
Engine half of Layr-Labs/d-inference#1207 (prefix-cache hit-rate fix set). The superproject pins this repository's
main; once this merges, that PR re-pins to the newmainhead.Why
Complete prefix checkpoints for the historical-attention layouts (GPT-OSS 20B, Gemma 4) were captured only where every prefill chunk below the boundary had the same size, and only two per donor (first and latest). A request that gained or lost decode company mid-prompt changed chunk size and stopped capturing, so production hits restored about 2,200 tokens of 6,700-token prompts, and a solo Gemma prompt under 2,048 tokens (46% of Gemma traffic) never checkpointed at all.
What changes
recordHistorical,historicalCheckpointStrideTokens). A checkpoint atpis the owning full rows[0, p)plus each sliding owner's[p−W, p); nothing in it depends on chunk geometry. Packed rows, preemption and media still disarm capture. Recurrent (Qwen) layouts keep the uniform-chunk rule.CBv2HistoricalCheckpointRetention): the first boundary, the fork targetfloor_1024(prefixCheckpointTargetTokens)when the request carries the coordinator's observed repeated-prefix length, and the rolling latest. A target within one stride of the final latest is dropped at publication. Without a hint: first + latest.CBv2HistoricalStagingCap): staged windows across all donors, including those still being published, may hold at most 1/8 of slot capacity, read at the moment of use; the per-donor 1/16 budget sheds in the same order (first, then target, latest always kept). Worst case for four concurrent Gemma donors on a 16 GB slot is 1.95 GB against 2.60 GB before.CBv2PrefixReusePlan.excludesPackedPrefill); manifests carrychunkSize = 1024as alignment only.CBv2Request.prefixCheckpointTargetTokens(nil = no hint).Before / after
flowchart LR classDef bad fill:#FEE2E2,stroke:#B91C1C,color:#7F1D1D classDef good fill:#DCFCE7,stroke:#15803D,color:#14532D classDef neutral fill:#F1F5F9,stroke:#475569,color:#0F172A subgraph B["Before"] direction LR B1["Prefill chunk sizes<br/>2048 solo → 512 with company"]:::neutral --> B2["record(): chunk size changed<br/>→ capture disarmed"]:::bad --> B3["Checkpoints: first + latest<br/>latest stuck at 2,048"]:::bad end subgraph A["After"] direction LR A1["Same chunk sizes"]:::neutral --> A2["recordHistorical(): every 1,024<br/>boundary the range covers"]:::good --> A3["Retain first, fork target, latest<br/>under a slot-wide cap"]:::good endVerification
CBv2HistoricalCheckpointGeometryTests(8),CBv2HistoricalCheckpointRetentionTests(13),HistoricalWindowCheckpointEngineTests(10),CBv2PackedPrefillTests(16),CBv2CompleteCheckpointEngineTests(6),Qwen35PagedCompleteCheckpointTests(2),GPTOSSPrefillOutputTestsandCBv2Gemma4ScheduledPrefillTests: 73 XCTest + 14 Swift Testing tests, 0 failures. Each behavior change has a test that fails without it (the packed-prefill guard fails three assertions with the flag off).[1024, 6144]and the repeat restores 6,144 of 6,413 tokens withsameText=true(the old rule kept 2,048); a fork prompt sharing 2,327 tokens restores the hinted 2,048 boundary; a growing conversation restores the previous turn's deepest. Live Gemma reconstruction restores 6,144 of 6,846,sameText=true.Measured sizes: gpt-oss-20b K/V is float32 on 23 of 24 layers, 49,152 B per token, 308.0 MB file at 6,144 tokens, 6.03 MB window per checkpoint; Gemma 4 window 209.7 MB per checkpoint.
🤖 Generated with Claude Code